<template>
	<view class="mainContent">
		<view class="twoSecond">
			<view class="left">
				<p class="flex">饮食时间：
					<picker mode="date" :start="start" :end="end"  :disabled="status == 1"  @change="bindDateChange">
						<view class="date">{{createdDate || '选择饮食时间'}}</view>
					</picker> 
				</p>
				<view><text>饮食备注：</text><input type="text" @click="status == 1?false:popUp('remarksColumns')" v-model="remarks" :disabled="status == 1"></view>			
			</view>
			<view class="right" v-if="status != 1">
				<!-- <view class="save" @click="seveAdd(1)">保存</view> -->
				<view class="saveSure" @click="seveAdd(1)">确认存档</view>
			</view>
		</view>
		<view class="uploadBox">
			<u-upload  ref="uUpload" :header="header" :form-data="{type:12}" :action="action" :file-list="imgUrls" :deletable="status != 1" @on-uploaded="uploaded"></u-upload>
		</view>
		<u-modal v-model="showModal" :show-cancel-button="true" @confirm="Modalconfirm" :content="showContent"></u-modal>
		<!-- 下拉框 -->
		<u-select v-model="dropDownshow" @confirm="dropDownconfirm" :list="defaultArr"></u-select>
	</view>
</template>

<script>
	import http from '@/common/http/interface.js'
	import {getUrlParam} from "@/static/js/filter.js"
	export default {
		data () {
			const currentDate = this.getDate({
				format: true
			})
			return {
				start: '',
				end: currentDate,
				action: http.config.baseUrl+'/File/multipleFiles',
				header:{
					"Token": localStorage.token
				},
				status:'', 
				showModal:false,
				showContent:'',
				remarks: '',
				createdDate: '',
				dropDownshow:false,	
				defaultArr: [],
				imgUrls: [],
				remarksColumns: [{
					value: '1',
					label: '早餐'
				},
				{
					value: '2',
					label: '午餐'
				},
				{
					value: '3',
					label: '晚餐'
				},
				{
					value: '4',
					label: '加餐'
				}],
				appDietRecordImageEntities: [],
				id: '',
				status:''
			}
		},
		onLoad() {
			this.id = getUrlParam('id');
			if(this.id) {
				this.$api.AppDietRecordInfo(this.id).then((res) => {
					this.createdDate = res.data.result.createdDate
					this.remarks = res.data.result.remarks
					this.status = res.data.result.status
					this.imgUrls = res.data.result && res.data.result.appDietRecordImageEntities.map((a)=>{
						return {url:a.image}
					})
				})
			}
		},
		methods: {
			uploaded(lists, name){
				this.imgUrls = lists.map((a)=>{
					if(a.file){
						a.url = a.response.result[0]
					}
					return a
				})
			},
			seveAdd(e) {
				if(!this.createdDate){
					uni.showToast({
						title:'请填写时间！',
						icon:'none'
					})
					return;
				}
				if(!this.remarks){
					uni.showToast({
						title:'请选择饮食备注！',
						icon:'none'
					})
					return;
				}
				if(this.imgUrls.length <= 0){
					uni.showToast({
						title:'至少上传一张图片！',
						icon:'none'
					})
					return;
				}
				this.showModal = true
				this.showContent = '确认信息无误，存档后不可修改。'
			},
			Modalconfirm(){
				
				this.appDietRecordImageEntities = this.imgUrls.map((a)=>{
					if(a.response){
						return a.response.result[0]
					}else{
						return a.url
					}
				}) 	
				
				const imageArr = this.appDietRecordImageEntities.map((a) => {
					return {image : a}
				});
				const appDietRecordVo = {	
					appDietRecordImageEntities: imageArr,
					remarks : this.remarks,
					createdDate : this.createdDate,
					status : 1
				}
				
				
				this.$api.AppDietRecordSave(appDietRecordVo).then((res) => {
					if(res.data.returnCode == '200') {
						uni.showToast({
							title: '保存成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url:'/pages/monitor/dailyMonitoring/dietList'
							})
						},1000)
					}
				})
			},
			// 选择弹出
			popUp(e) {
				this.dropDownshow = true;
				this.confirmButton = e;
				this.defaultArr = e == 'remarksColumns' ? this.remarksColumns : []		
			},
			// 下拉框确定
			dropDownconfirm(e) {
				this.confirmButton == 'remarksColumns' ? this.remarks = e[0].label : ''
			},
			// 获取当前时间
			getDate(type) {
			   const date = new Date();
			   let year = date.getFullYear();
			   let month = date.getMonth() + 1;
			   let day = date.getDate();
			  
			   if (type === 'start') {
				year = year - 60;
			   } else if (type === 'end') {
				year = year + 2;
			   }
			   month = month > 9 ? month : '0' + month;;
			   day = day > 9 ? day : '0' + day;
			   return `${year}-${month}-${day}`;
			},
			// 日期
			bindDateChange(e) {  //日期选择
				this.createdDate = e.target.value;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.mainContent {
		width: 750upx;
		min-height: calc(100vh - 44px);
		background-color: #FFFFFF;
		.twoSecond {
			width: 750upx;
			height: 176upx;		
			background: #F5FFFB;
			// box-shadow: 0upx 4upx 30upx 0upx rgba(222, 222, 222, 0.3);
			display: flex;
			position: fixed;
			top: 44px;
			align-items: center;
			padding: 0 24upx;
			box-sizing: border-box;
			z-index: 200;
			.left {
				flex: 1;
				width: 702upx;
				view {
					display: flex;
					align-items: center;
					text {
						color: #333333;
						font-size: 30upx;
					}				
					input {
						width: 319upx;
						height: 46upx;
						padding-left: 10upx;
						border: 1upx solid #E4E4E4;
						border-radius: 6upx;
						display: inline-block;
						margin: 10upx 0;
					}
				}
			}
			.right {
				height: 108upx;
				view {
					width: 174upx;
					height: 46upx;
					background: #FF8F23;
					border-radius: 8upx;
					color: #FFFFFF;
					font-size: 30upx;
					text-align: center;
					line-height: 46upx;
				}
				.saveSure {
					background-color: #38CB97;
					line-height: 108upx;
					height: 100%;
				}
			}
		}
		.uploadBox {
			margin-top: 200rpx;
			width: 750upx;
			padding: 0 60upx;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;
			.u-upload{
				width: 100%;
				display: flex;
				justify-content: space-between;
				/deep/ .u-list-item {
					width: 266upx !important;
					height: 350upx !important;
					background: #EFEFEF;
					border-radius: 10upx;
					position: relative;
				}
			}
		}
	}
	.flex{
		display: flex;
		font-size: 30rpx;
		.date{
			width: 334upx;
			height: 50upx;
			padding-left: 10upx;
			border: 1upx solid #E4E4E4;
			border-radius: 6upx;
		}
	}
</style>

